package process

import (
	"github.com/beego/beego/v2/core/logs"
	"github.com/go-redis/redis"
	"time"
)

var (
	gRedisClient *redis.Client
)

func CacheCllientInit(addr, passwd string) {

	gRedisClient = redis.NewClient(&redis.Options{
		Addr:         addr,
		Password:     passwd,
		PoolSize:     2,
		MinIdleConns: 1,
		IdleTimeout:  180 * time.Second,
		DB:           2,
	})

	res := gRedisClient.Ping()
	if res.Err() != nil {
		logs.Error("redis connect failed:%v", res.String())
		gRedisClient = nil
	} else {
		logs.Info("redis 连接成功,启用外呼缓存......")
	}
}

func saveCallInfo(appId, serviceId, number string) {

	if gRedisClient != nil {
		gRedisClient.HSet(number, appId, serviceId)
		gRedisClient.ExpireAt(number, time.Now().Add(8*time.Hour))
	}

}

func getCallInfo(appId, number string) string {

	if gRedisClient != nil {
		result := gRedisClient.HGet(number, appId)
		return result.Val()
	}
	return ""
}
